package com.blb.service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.blb.entity.Menu;
import com.blb.entity.MenuExample;
import com.blb.entity.User;
import com.blb.mapper.MenuExtMapper;
import com.blb.mapper.MenuMapper;
import com.blb.utils.NowUserUtils;

@Service
public class MenuService {


    @Autowired
    private MenuMapper menuMapper;

    @Autowired
    private MenuExtMapper menuExtMapper;

    /**
     * 获取当前菜单和当前用户的按钮
     *
     * @param menuid
     * @return
     */
    public List<Menu> getBts(String menuid) {
        User user = NowUserUtils.getNowUser();
        List<Menu> list = menuExtMapper.getButtenByuserid(user.getId(), menuid);
        return list;
    }

    /**
     * 查询所有菜单
     *
     * @return
     * @throws IOException
     */
    public List<Menu> page() throws IOException {
        List<Menu> list = menuMapper.selectByExample(null);
        return list;
    }

    /**
     * 获取父级菜单
     *
     * @return
     * @throws IOException
     */
    public List<Map<String, Object>> getParentMenu() throws IOException {
        MenuExample menuExample = new MenuExample();
        menuExample.createCriteria().andPidEqualTo("0");

        //查询所有目录层次的菜单
        List<Menu> list = menuMapper.selectByExample(menuExample);

        //menus就是下拉框需要的数据格式
        List<Map<String, Object>> menus = new ArrayList<Map<String, Object>>();

        for (Menu m : list) {
            HashMap<String, Object> map = new HashMap<String, Object>();
            map.put("id", m.getId());
            map.put("name", m.getMenuname());
            map.put("open", true);
            map.put("checked", false);

            //根据当前菜单id查询该菜单的子菜单
            MenuExample menuExample2 = new MenuExample();
            menuExample2.createCriteria().andPidEqualTo(m.getId());
            List<Menu> list2 = menuMapper.selectByExample(menuExample2);
            //将子菜单转化格式
            List<Map<String, Object>> childrens = new ArrayList<Map<String, Object>>();
            for (Menu mm : list2) {
                HashMap<String, Object> map2 = new HashMap<String, Object>();
                map2.put("id", mm.getId());
                map2.put("name", mm.getMenuname());
                map2.put("open", true);
                map2.put("checked", false);
                childrens.add(map2);
            }
            map.put("children", childrens);
            menus.add(map);
        }
        return menus;
    }

    /**
     * 添加
     *
     * @param menu
     * @throws IOException
     */
    public void add(Menu menu) throws IOException {
        menuMapper.insertSelective(menu);
    }


    /**
     * 修改
     *
     * @param menu
     * @throws IOException
     */
    public void update(Menu menu) throws IOException {
        menuMapper.updateByPrimaryKeySelective(menu);
    }

    /**
     * 删除
     *
     * @param id
     * @throws IOException
     */
    public String delete(String id) throws IOException {
        MenuExample menuExample = new MenuExample();

        menuExample.createCriteria().andPidEqualTo(id);
        List<Menu> list = menuMapper.selectByExample(menuExample);
        if (!list.isEmpty()) {
            return "该目录下还有子目录，不允许删除";
        }

        menuMapper.deleteByPrimaryKey(id);


        return "";
    }
}
